package com.sgb.goods.viewmodel;

import androidx.lifecycle.MutableLiveData;

import com.alibaba.android.arouter.launcher.ARouter;
import com.sgb.goods.api.GoodsCallBack;
import com.sgb.goods.api.GoodsNet;
import com.sgb.goods.entity.VipPriceMangeListEntity;
import com.sgb.goods.utils.Constants;
import com.sgb.goods.utils.base.viewmodel.BaseViewModel;
import com.sgb.goods.view.adapter.ChoiceVipPriceGoodsAdapter;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.ListResponse;

import java.util.HashMap;
import java.util.List;

/**
 * 选择会员价商品
 * @author cyj
 */
public class ChoiceVipPriceGoodsViewModel extends BaseViewModel {

    private int page = 1;
    public ChoiceVipPriceGoodsAdapter adapter;
    public MutableLiveData<List<VipPriceMangeListEntity>> listData = new MutableLiveData<>();
    public MutableLiveData<String> dialog_show_text = new MutableLiveData<>();
    public MutableLiveData<String> ok = new MutableLiveData<>();

    public ChoiceVipPriceGoodsViewModel() {
        adapter = new ChoiceVipPriceGoodsAdapter();
    }

    /** 设置会员价-商品列表 */
    public void getData(boolean isLoadMore) {
        page = isLoadMore ? page + 1 : 1;

        HashMap<String, Object> body = new HashMap<>();
        body.put("page", page);
        body.put("limit", Constants.PAGE_SIZE_20);

        showLoading();
        GoodsNet.getInstance().getMemberPriceApi().getCanSetGoodsList(body).enqueue(new GoodsCallBack<BaseEntity<ListResponse<List<VipPriceMangeListEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<VipPriceMangeListEntity>>> data) {
                if(isLoadMore) {
                    adapter.addData(data.getData().getList());
                } else {
                    adapter.setNewData(data.getData().getList());
                }
                listData.postValue(data.getData().getList());
            }

            @Override
            public void onFail() {
                listData.postValue(null);
            }
        });
    }

    /** 检查商品是否有sku。根据商品获取sku列表 */
    public void getGoodsType() {
        showLoading();
        GoodsNet.getInstance().getMemberPriceApi().getGoodsSku(adapter.getData().get(adapter.position).id).enqueue(
                new GoodsCallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                if ("ok".equals(data.getState())) {
                    // ok.postValue(itemId + "");
                    ARouter.getInstance().build(Constants.ARouter.AR_ADD_MEMBER_GOODS)
                            .withLong("itemId", adapter.getData().get(adapter.position).id).navigation();
                    finishActivity();
                } else {
                    dialog_show_text.postValue(data.getMsg());
                }
            }
        });
    }
}
